package com.zjcode.cdcer.service.impl;

import com.chinatechstar.component.commons.entity.MPage;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.service.BaseService;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjcode.cdcer.mapper.AnnualReportSummaryMapper;
import com.zjcode.cdcer.mapper.TkmSysOrgMapper;
import com.zjcode.cdcer.mapper.TkmSysUserMapper;
import com.zjcode.cdcer.model.AnnualReportSummary;
import com.zjcode.cdcer.model.TkmSysOrg;
import com.zjcode.cdcer.model.TkmSysUser;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.AnnualReportSummaryService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ZQM
 * @date 2023/10/12 15:37
 */
@Service
public class AnnualReportSummaryServiceImpl implements AnnualReportSummaryService, BaseService {

    @Autowired
    private AnnualReportSummaryMapper annualReportSummaryMapper;

    @Autowired
    private TkmSysOrgMapper orgMapper;

    @Autowired
    private TkmSysUserMapper userMapper;

    @Override
    public JsonObj add(PageData pageData) {
        Long userId = Long.valueOf(pageData.get("user").toString());
        // 根据userId查询当前登录用户信息
        TkmSysUser userInfo = userMapper.selectByPrimaryKey(userId);
        // 根据当前登录用户所属机构id查询整机构数据
        TkmSysOrg sysOrg = orgMapper.selectByPrimaryKey(userInfo.getOrgId());
        // 新增数据组建
        AnnualReportSummary annualReportSummary = new AnnualReportSummary(){{
            setId(get32UUID());
            setProvinceCode(sysOrg.getProvinceCode());
            setProvinceName(sysOrg.getProvinceName());
            setCityCode(sysOrg.getCityCode());
            setCityName(sysOrg.getCityName());
            setDistrictCode(sysOrg.getRegionCode());
            setDistrictName(sysOrg.getRegionName());
            setInstitutionCode(String.valueOf(sysOrg.getId()));
            setInstitutionName(sysOrg.getOrgName());
            setScreeningYear(Integer.valueOf(pageData.get("screeningYear").toString()));
            setFileName((String) pageData.get("fileName"));
            setUrl((String) pageData.get("url"));
            setAbsoluteUrl((String) pageData.get("absoluteUrl"));
            setType(Integer.valueOf(pageData.get("type").toString()));
            setExtFlag(sysOrg.getAncestors() + "," + sysOrg.getId());
        }};
        Integer i = annualReportSummaryMapper.add(annualReportSummary);
        if (i > 0) {
            return JsonObj.success("创建成功");
        } else {
            return JsonObj.error("创建失败");
        }
    }

    @Override
    public JsonObj selById(String id) {
        Map<String, Object> map = new HashMap<>();
        AnnualReportSummary annualReportSummary = annualReportSummaryMapper.selById(id);
        map.put("id", annualReportSummary.getId());
        map.put("provinceCode", annualReportSummary.getProvinceCode());
        map.put("provinceName", annualReportSummary.getProvinceName());
        map.put("cityCode", annualReportSummary.getCityCode());
        map.put("cityName", annualReportSummary.getCityName());
        map.put("districtCode", annualReportSummary.getDistrictCode());
        map.put("districtName", annualReportSummary.getDistrictName());
        map.put("institutionCode", annualReportSummary.getInstitutionCode());
        map.put("institutionName", annualReportSummary.getInstitutionName());
        map.put("screeningYear", annualReportSummary.getScreeningYear());
        map.put("fileName", annualReportSummary.getFileName());
        map.put("url", annualReportSummary.getUrl());
        map.put("absoluteUrl", annualReportSummary.getAbsoluteUrl());
        map.put("type", annualReportSummary.getType());
        map.put("extFlag", annualReportSummary.getExtFlag());
        map.put("createTime", annualReportSummary.getCreateTime());
        map.put("updateTime", annualReportSummary.getUpdateTime());
        return JsonObj.success(map);
    }

    @Override
    public Map<String, Object> selList(PageData pageData) {
        // 获取当前用户信息
        Long userId = Long.valueOf(pageData.get("user").toString());
        TkmSysUser userInfo = userMapper.selectByPrimaryKey(userId);
        TkmSysOrg orgInfo = orgMapper.selectByPrimaryKey(userInfo.getOrgId());
        pageData.put("ancestors", orgInfo.getAncestors());
        // 解析分页参数
        MPage mPage = getPageInfo(pageData);
        Page<Object> page = PageHelper.startPage(mPage.getCurrentPage(), mPage.getPageSize());
        String cityCode = ObjectUtils.isEmpty(pageData.getString("cityCode") ) ? "" : pageData.getString("cityCode") +"00";
        pageData.put("cityCode", cityCode);
        List<LinkedHashMap<String, Object>> list = annualReportSummaryMapper.selList(pageData);
        return PaginationBuilder.buildResult(list,
                page.getTotal(),
                mPage.getCurrentPage(),
                mPage.getPageSize());
    }

    @Override
    public JsonObj del(String id) {
        Integer i = annualReportSummaryMapper.del(id);
        if (i > 0) {
            return JsonObj.success("删除成功");
        } else {
            return JsonObj.error("删除失败");
        }
    }

    @Override
    public Map<String, Object> showAllFile(Long userId, Integer type, String screeningYear) {
        TkmSysUser userInfo = userMapper.selectByPrimaryKey(userId);
        List<LinkedHashMap<String, Object>> returnList = new ArrayList<>();
        returnList.addAll(annualReportSummaryMapper.show(String.valueOf(userInfo.getOrgId()), type, screeningYear));
        if (StringUtils.isEmpty(returnList)) {
            TkmSysOrg sysOrg = orgMapper.selectByPrimaryKey(userInfo.getOrgId());
            LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
            linkedHashMap.put("screening_year", screeningYear);
            linkedHashMap.put("institution_code", sysOrg.getId());
            linkedHashMap.put("institution_name", sysOrg.getOrgName());
            returnList.add(linkedHashMap);
        }
        return PaginationBuilder.buildResult(returnList);
    }
}
